package work.sby.web;

import work.sby.pojo.Brand;
import work.sby.service.BrandService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandService brandService = new BrandService();
 @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//处理post请求乱码
    request.setCharacterEncoding("UTF-8");

    //1.接收表单数据，封装为Brand对象
        String brandName = request.getParameter("brandName");
     String companyName = request.getParameter("companyName");
     String price = request.getParameter("price");
     String description = request.getParameter("description");
//     String status = request.getParameter("status");

     //2.封装为Brand对象
     Brand brand = new Brand();
     brand.setBrandName(brandName);
     brand.setCompanyName(companyName);
     brand.setPrice(Integer.parseInt(price));
     brand.setDescription(description);
//     brand.setStatus(Integer.parseInt(status));

     //3.调用service层方法，添加到数据库
     brandService.add(brand);
    //4发到页面
    request.getRequestDispatcher("/selectAllServlet").forward(request,response);

}
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
